package edu.whu.demo.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import edu.whu.demo.domain.Message;
import edu.whu.demo.domain.MessageDTO;
import edu.whu.demo.exception.MessageException;
import edu.whu.demo.service.IMessageService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2022-12-15
 */
@RestController
@RequestMapping("/message")
public class MessageController {

    @Autowired
    IMessageService messageService;

    @ApiOperation("查询最新消息简表，按活动id分行")
    @GetMapping("/outer")
    public IPage<MessageDTO> getOuterMessage(@ApiParam("消息类型") String type,
                                        @ApiParam("用户id") Long toUid,
                                        @ApiParam("页码")@RequestParam(defaultValue = "0")Integer pageNum,
                                        @ApiParam("每页记录数") @RequestParam(defaultValue = "10")Integer pageSize)
    {
        return messageService.getOuterMessage(type, toUid, pageNum, pageSize);
    }

    @ApiOperation("查询消息列表，一个消息一行")
    @GetMapping("/single")
    public IPage<MessageDTO> getMessage(@ApiParam("消息类型") String type,
                                        @ApiParam("用户id") Long toUid,
                                        @ApiParam("活动id") Long activityId,
                                        @ApiParam("是否已读") Integer isRead,
                                        @ApiParam("页码")@RequestParam(defaultValue = "0")Integer pageNum,
                                        @ApiParam("每页记录数") @RequestParam(defaultValue = "10")Integer pageSize)
    {
        Map<String,Object> condition=new HashMap<>();
        if(type!=null) {
            condition.put("type", type);
        }
        if(toUid!=null) {
            condition.put("toUid", toUid);
        }
        if(activityId!=null) {
            condition.put("activityId", activityId);
        }
        if(isRead!=null) {
            condition.put("isRead", isRead);
        }
        return messageService.getMessage(condition, pageNum, pageSize);
    }

    @ApiOperation("将消息设为已读")
    @PutMapping("")
    public void setRead(@RequestBody Message message) throws MessageException
    {

        Map<String,Object> condition=new HashMap<>();
        if(message.getId()!=null){
            condition.put("id",message.getId());
        }
        if(message.getType()!=null) {
            condition.put("type", message.getType());
        }
        if(message.getToUid()!=null) {
            condition.put("toUid", message.getToUid());
        }
        if(message.getActivityId()!=null) {
            condition.put("activityId", message.getActivityId());
        }
        messageService.setRead(condition);
    }

}

